<template>
  <div class="app-container">
    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="90px" size="small">
      <el-form-item label="回收点类型" prop="channelId">
        <el-select v-model="queryParams.channelId" placeholder="请选择回收点类型" clearable>
          <el-option v-for="dict in getDictDatas(DICT_TYPE.sys_channel_type)" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)" />
        </el-select>
      </el-form-item>
      <el-form-item label="回收点名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入回收点名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="联系人" prop="userName">
        <el-input v-model="queryParams.userName" placeholder="请输入联系人" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['pekingfish:recycleconfig:add']">新增</el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 列表 -->
    <el-table v-loading="loading" :data="list">
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="130">
        <template v-slot="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['pekingfish:recycleconfig:update']">修改</el-button>
        </template>
      </el-table-column>
      <el-table-column label="回收点类型" align="center" prop="channelId">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.sys_channel_type" :value="scope.row.channelId" />
        </template>
      </el-table-column>
      <el-table-column label="回收点名称" align="center" prop="name" />
      <el-table-column label="回收点地址" align="center" prop="address" show-overflow-tooltip />
      <el-table-column label="联系人" align="center" prop="userName" />
      <el-table-column label="联系方式" align="center" prop="phone" />
      <!-- <el-table-column label="状态" align="center" prop="status" width="100">
        <template v-slot="scope">
          <el-switch
            @change="changeSwitch(scope.row)"
            v-model="scope.row.status"
            :active-value="0"
            :inactive-value="1"
          ></el-switch>
        </template>
      </el-table-column> -->
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="title" :visible.sync="open" width="700px" v-dialogDrag append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="回收点类型" prop="channelId">
          <el-select style="width:100%" v-model="form.channelId" placeholder="请选择回收点类型" clearable>
            <el-option v-for="dict in getDictDatas(DICT_TYPE.sys_channel_type)" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)" />
          </el-select>
        </el-form-item>
        <el-form-item label="回收点名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" maxlength="50" />
        </el-form-item>
        <el-form-item label="回收点地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址" maxlength="100" />
        </el-form-item>
        <el-form-item label="联系人" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入联系人" maxlength="50" />
        </el-form-item>
        <el-form-item label="联系方式" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入联系方式" maxlength="11" />
        </el-form-item>
        <!-- <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio v-for="dict in this.getDictDatas(DICT_TYPE.pc_enable_disable_status)" :key="dict.value" :label="parseInt(dict.value)">{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="loading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { createRecycleConfig, updateRecycleConfig, getRecycleConfigPage } from "@/api/configuration/recycleConfig";

export default {
  name: "RecycleConfig",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 回收点配置列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        channelId: null,
        name: null,
        address: null,
        userName: null,
        phone: null,
        status: null,
        createTime: [],
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        channelId: [{ required: true, message: "回收点类型不能为空", trigger: "blur,change" }],
        name: [{ required: true, message: "名称不能为空", trigger: "blur" }],
        address: [{ required: true, message: "地址不能为空", trigger: "blur" }],
        userName: [{ required: true, message: "联系人不能为空", trigger: "blur" }],
        phone: [{ required: true, message: "联系方式不能为空", trigger: "blur" }],

      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 执行查询
      getRecycleConfigPage(this.queryParams).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        channelId: undefined,
        name: undefined,
        address: undefined,
        userName: undefined,
        phone: undefined,
        status: 1,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加回收点";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.form = row;
      this.open = true;
      this.title = "修改回收点";
    },
    // 状态
    changeSwitch(data) {
      updateRecycleConfig(data).then(res => {
        this.$message({
          message: '操作成功',
          type: 'success'
        });
        this.getList()
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (!valid) {
          return;
        }
        // 修改的提交
        this.loading = true;
        if (this.form.id) {
          updateRecycleConfig(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
            this.loading = false
          }).catch(f => {
            this.loading = false
          });

        } else {
          // 添加的提交
          createRecycleConfig(this.form).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.open = false;
            this.getList();
            this.loading = false
          }).catch(f => {
            this.loading = false
          });
        }

      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$modal.confirm('是否确认删除回收点配置编号为"' + id + '"的数据项?').then(function () {
        return deleteRecycleConfig(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = { ...this.queryParams };
      params.pageNo = undefined;
      params.pageSize = undefined;
      this.$modal.confirm('是否确认导出所有回收点配置数据项?').then(() => {
        this.exportLoading = true;
        return exportRecycleConfigExcel(params);
      }).then(response => {
        this.$download.excel(response, '回收点配置.xls');
        this.exportLoading = false;
      }).catch(() => { });
    }
  }
};
</script>
